<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'choujiang';
//exi('xxxx','error');
$return=array();
//查询当前粉丝是否关注当前公众号
$fans = cfc('core')->get_fans($_SESSION['openid']);
$fansinfo = pdo_get('core_fans',array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
$follow = $fansinfo['follow'];
if($_RQ['op']=='choujiang'){
	if($_RQ['rid']){
		$id=intval($_RQ['rid']);
	}
	if(!$id){
		exi('参数错误！','error');
	}
	//查询对应活动的参数
	$item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//获取当前时间
	$time = time();	
	
	//实际参与该活动的人数
	$playerNum = pdo_count('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//总人数=实际人数+虚拟人数
	$playerNums = $playerNum + $item['virtualRegNum'];
	 
	//活动奖项数据 转成数组格式
	$award = json_decode($item['award'],true);
	//活动奖项数量
	$awardNum = count($award);
	//奖品总数量
	for($x=0; $x<$awardNum; $x++){
		$allPrize += $award[$x]['prizeNum'];
	} 
    //按排名取与奖品数量等同的玩家(被拉黑玩家不在计量范围)	
    if($allPrize){
		$list = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$allPrize);
    }
	//排行榜
	if($item['isRank'] == 2){
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$item['rank']);
	}
	//该活动玩家积分排名(前30)
	else{
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,30');
	}

	if($list){
		$paiming = 1;
		foreach($list as &$row){
			$row['paiming']=$paiming;
			$paiming = $paiming+1;	
		}
	}
	if($rankList){
		$paiming = 1;
		foreach($rankList as &$row){
			$row['paiming']=$paiming;
			$paiming = $paiming+1;
		}
	}
    //当前玩家信息
	$play = pdo_get('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid']));
	//当前玩家得分
	$score = pdo_getcolumn('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'bestScore');
	$return['award'] = array('action'=>'award','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['notice'] = array('action'=>'notice','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['info'] = array('action'=>'info','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$item['starttime2']=date('Y-m-d H:m:s',$item['starttime']);
	$item['endtime2']=date('Y-m-d H:m:s',$item['endtime']);
	//判断活动名额和状态
	$wait_id=$this->model()->is_shopgame($id);
	$jsondata=$this->model()->get_checkgame($item,$wait_id);
	$return['errorCode'] = $jsondata['errorCode'];
	$errorCode = $jsondata['errorCode'];
	$return['errorMsg']=$jsondata['errorMsg'];
	if($item['gameTypeSetting'] == 1){
		if($errorCode==0){
			$ap=intval($_RQ['ap'])?:8;
			$lastall=0;
			for($i=1;$i<=$ap;$i++){
				$ak=$i-1;
				$arr['p1'] = 100 * $award[$ak]['rate'];
				$lastall +=$arr['p1'];
			}
			$arr['p8'] = 10000 - $lastall;
			$luckkey = $this->get_rand($award);
			$errorCode = 1;
			//查询中奖数量
			if($luckkey == 100){
				$result = 'p'.$ap;
				$prize=$ap;
				$errorCode = 1;
			}else{
				$code1 = $this->make_coupon_card();	
				$awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'fansID' => $play['fansID'],'uniacid'=>$_SESSION['uniacid'],'awardLevel' => $luckkey));
				$result = 'p'.($luckkey+1);
				$prize=$luckkey+1;
				if($awardnum>=$award[$luckkey]['prizeNum']){
					$result = 'p'.($luckkey+2);
					$prize=$luckkey+2;
				}else{
						$errorCode = 0;
						$return['status'] = 1;
						$awarddata = array(
							'rid' => $id,
							'fansID' => $play['fansID'],
							'award' => $award[$luckkey]['prizeName'],
							'awardinfo'=>$play['name'],
							'uniacid'=>$_SESSION['uniacid'],
							'awardLevel' => $luckkey,
							'prizeType'=>$award[$luckkey]['prizeType'],
							'awardjifen'=>$award[$luckkey]['jifen'],
							'awardvalue'=>ijson_encode($award[$luckkey]),
							'createtime' => time(),
							'ausername'=>$play['ausername'],
							'address'=>$play['address'],
							'aphone'=>$play['tel'],
							'code1'=>$code1,
							'awardstatus'=>0,
							'awardTime'=>time()
					);
					//中奖后，存入数据
					$this->insert('award',$awarddata);
					$aid=pdo_insertid();
					if($awarddata['prizeType'] >=2 && $awarddata['prizeType']<=6){
						$this->model('model')->get_award($aid,true);
					}
				}
				$title = '真实物品券';
				$type = '真实物品券';
				$value = 1;
				$award2 = array(
									'title' => $awarddata['award'],
									'value' => '22',
									'type' => '1',
									'remsg' => !empty($remsg) ? $remsg : ' ',
									'point' => '2',
									'point_name' => '11',
									'give_point' => '1',
									'give_name' => '444444',
									'level' => '4',
									'detail_url' => 'xxxx'
				);
	             $return['errorCode'] = $errorCode;
	             $return['data'] = $award2;
				 $this->model()->up_shopgame($wait_id);
			}
			 //记录抽奖次数
			 if($item['isLimitDrawTimes'] == 1){
					$totalnum = pdo_getcolumn('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'totalnum');			
					$totalnum = $totalnum+1;
					pdo_update('jy_h5game_fans',array('totalnum'=>$totalnum),array('fansID' => $play['fansID'],'rid'=>$id,'uniacid'=>$_SESSION['uniacid']));		 
					
			}
		}
	}else{
		$errorCode=1;
		$return['errorMsg'] = '亲，管理员未设置本活动为抽奖类型，无法抽奖！';
	}
	$return['prize'] = $prize;
	$return['result'] = $result;
	$return['code'] = $errorCode;
	$return['item']=$item;
	$return['play']=$play;
	$return['fansinfo']=$fansinfo;
	$return['playerNums']=$playerNums;
	$return['list']=$list;
	$return['score']=$score;
	$return['rankList']=$rankList;
	$return['awarddata']=$awarddata;
	$return['success']=$errorCode;
	$return['awardlist']=$award;
	exi($return);
}